package com.module.app.utils

import android.content.Intent
import android.provider.Settings
import android.view.View
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.*
import com.module.app.pop.CommonPop
import com.module.base.utils.TimeUtils
import com.module.app.R
import com.module.frame.app.AppManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.net.URL

/**
 * 网络时间纠正
 */
class NetTimeUtils {
    companion object {
        fun onNetRectify() {
            GlobalScope.launch {
                kotlin.runCatching {
                    withContext(Dispatchers.IO) {
                        val url = URL("http://www.baidu.com") //取得资源对象
                        val uc = url.openConnection() //生成连接对象
                        uc.connect() //发出连接

                        val time = uc.date //取得网站日期时间
                        var errorTime: Boolean =
                            TimeUtils.getDiffMinute(time, System.currentTimeMillis()) > 20
                        return@withContext errorTime
                    }
                }.onSuccess {
                    if (it) {
                        val currentActivity = AppManager.getInstance().currentActivity()
                        //时间有偏差，重启应用
                        val commonDialog: com.module.app.pop.CommonPop = com.module.app.pop.CommonPop.Builder(currentActivity)
                            .setContent("您当前系统时间有误，无法正常使用该app，请矫正时间。\n请前往【日期时间设置】页面调整")
                            .setCancelable(false)
                            .setCanceledOnTouchOutside(false)
                            .setConfirmButton(R.string.go_to)
                            .setOnConfirmListener(View.OnClickListener {
                                //跳转到设置时间界面
                                AppManager.getInstance().currentActivity().startActivity(
                                    Intent(
                                        Settings.ACTION_DATE_SETTINGS
                                    )
                                )
                            }).create()
                        val lifecycleObserver: DefaultLifecycleObserver =
                            object : DefaultLifecycleObserver {
                                override fun onStop(owner: LifecycleOwner) {
                                    super.onStop(owner)
                                    if (commonDialog != null) {
                                        commonDialog.dismiss()
                                    }
                                }
                            }
                        if (currentActivity is FragmentActivity) {
                            currentActivity.lifecycle.addObserver(lifecycleObserver)
                        }
                        commonDialog.setOnDismissListener {
                            if (currentActivity is FragmentActivity) {
                                currentActivity.lifecycle.removeObserver(lifecycleObserver)
                            }
                        }
                        commonDialog.show()
                    }
                }.onFailure {

                }
            }
        }
    }
}